package com.mdms.jsf.managedbeans.menu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.mdms.dto.MdmMenuConf;
import com.mdms.init.GetDBResourcesCommand;
import com.mdms.init.GetMenuCommand;
import com.mdms.jsf.managedbeans.BaseMBean;
import com.mdms.logger.SimplyMDLogger;
import com.mdms.utils.FacesUtils;
import com.mdms.utils.MDmsRessources;

public class NavigationMenuMBean extends BaseMBean {

	private static final long serialVersionUID = 1L;
	private final static SimplyMDLogger logger = new SimplyMDLogger(NavigationMenuMBean.class);
	
	private List<MdmsMenuObject> menuMap;

	public class MdmsMenuObject{

		private String name;
		private int level;
		private String navigation;
		private String right;
		private List<MdmsMenuObject> subMenu;
		private Long id;

		public MdmsMenuObject(){
		}

		public MdmsMenuObject(String name,int level,String nav,String right){
			this.name=name;
			this.level=level;
			this.navigation=nav;
			this.right=right;
		}
		
		public String execute(){
			return getNavigation();
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getLevel() {
			return level;
		}

		public void setLevel(int level) {
			this.level = level;
		}

		public String getNavigation() {
			return navigation;
		}

		public void setNavigation(String navigation) {
			this.navigation = navigation;
		}

		public String getRight() {
			return right;
		}

		public void setRight(String right) {
			if(right==null){
				right="";
			}
			this.right = right;
		}

		public List<MdmsMenuObject> getSubMenu() {
			return subMenu;
		}

		public void setSubMenu(List<MdmsMenuObject> subMenu) {
			this.subMenu = subMenu;
		}

		public Long getId() {
			return id;
		}

		public void setId(Long id) {
			this.id = id;
		}
		
		
		
	}

	public NavigationMenuMBean(){

	}
	
	private void prepareMenuMap(){
		logger.debug("prepareMenuMap", "START");
		GetMenuCommand command = new GetMenuCommand();
		try{
			command.execute();
			if(command.errorCode==0){		
			menuMap=new ArrayList<MdmsMenuObject>();
			Map<String,MdmsMenuObject>menuMapTemp=new HashMap<String,MdmsMenuObject>();
				for (MdmMenuConf menuConf: command.getMenus()) {
					if(menuConf.getMdmMenuConf()==null){
						MdmsMenuObject object = new MdmsMenuObject();
						object.setLevel(0);
						object.setName(FacesUtils.getFromResources(menuConf.getMnu_res_code()));
						object.setRight(menuConf.getMnu_right());
						object.setSubMenu(new ArrayList<NavigationMenuMBean.MdmsMenuObject>());
						object.setId(menuConf.getMnu_id());
						menuMap.add(object);
					}
				}
				for (MdmMenuConf menuConf1: command.getMenus()) {
					if(menuConf1.getMdmMenuConf()!=null){
						MdmsMenuObject object = new MdmsMenuObject();
						object.setLevel(1);
						object.setName(FacesUtils.getFromResources(menuConf1.getMnu_res_code()));
						object.setNavigation(menuConf1.getMnu_navigation());
						object.setRight(menuConf1.getMnu_right());
						object.setId(menuConf1.getMnu_id());
						Long mainId=menuConf1.getMdmMenuConf().getMnu_id();
						for (MdmsMenuObject iterable : menuMap) {
							if(mainId.equals(iterable.getId())){
								iterable.getSubMenu().add(object);
							}
						}				
					}
				}
			}
		}catch(Exception e){
			logger.error("prepareMenuMap", e);
		}
		
		logger.debug("prepareMenuMap", "END");
	}

	public List<MdmsMenuObject> getMenuMap() {
		if(menuMap==null){
			prepareMenuMap();
		}
		return menuMap;
	}

	public void setMenuMap(List<MdmsMenuObject> menuMap) {
		this.menuMap = menuMap;
	}

	
	
	
	
	

}
